import React from 'react';
import type { MenuDataItem } from '@ant-design/pro-layout';


export const  fixMenuItemIcon = (menus: MenuDataItem[]): MenuDataItem[] => {
   menus.forEach((item) => {
      const { icon, routes } = item
      if (typeof icon === 'string' && icon !== null) {
         const fixIconName = icon.slice(0, 1).toLocaleUpperCase() + icon.slice(1) + iconType
         // eslint-disable-next-line no-param-reassign
         item.icon = React.createElement(allIcons[fixIconName] || allIcons[icon])
      }
      // eslint-disable-next-line no-param-reassign,@typescript-eslint/no-unused-expressions
      routes && routes.length > 0 ? item.routes = fixMenuItemIcon(routes) : null
   });
   return menus
}

export function mergeRoutes(routes) {
    if (!Array.isArray(routes)) return [];
    return routes.map(route => {
      let module = route.component;
//       console.log('module',module)
      if (route.component) {
        route.component = (component => {
          if (typeof component === 'object') {
            console.log('1111')
            return module;
          }
          // 封装一个异步组件
          return require(module)
        })(route.component);
      }
      if (route.routes) {
        route.routes = mergeRoutes(route.routes);
      }
      return route;
    });
  }
